# CCNCore::Provision::Application::spool
# Copyright (C) 2006  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

#
# For more information: http://carrierclass.net/
#
use strict; use warnings;

package CCNCore::Provision::Application::spool;
use base qw(CCNCore::Provision::Base);
use CCN::Text;
use CCNCore::Scripting;

sub run()
{
    my $self = shift;
    return if $self->p->force_manager;

    COMMIT_MODULE(ref($self));
}

sub cmd()
{
    use CCNCore::IPC::Unspool;
    return CCNCore::IPC::Unspool::cmd();
}

sub commit()
{
    my $self = shift;
    # Restart unspool. This is one way to do it.
    # IF_CHANGED '' => CCNCore::Home::manager.'/bin/spool.pl restart-unspool';
    # Otherwise do a "kill -HUP `cat /home/config/local/spool/.running`".
    my $ps = content_of(CCNCore::Home::config.'/local/spool/.Unspool.running');
    if(defined $ps)
    {
        $self->log("Restarting Unspool (PID=$ps)\n");
        $ps = int($ps);
        kill 'HUP', $ps if $ps;
    }

    # Respawn a new unspool if the current one died
    use CCNCore::Scripting;
    CCNCore::Scripting::CRONTAB q(*/5 * * * * ).$self->cmd;
}

1;
